import { reactive,inject } from "vue";
import { Api } from "../../../api/api.js";
import { ElNotification } from 'element-plus';
import { GJ_nowTime } from "../../../../static/js/tool.js";
function dataAddJS() {
    let getinfo = inject("getinfo");
    let status = reactive({
        userinfo:JSON.parse(window.sessionStorage.getItem("userinfo")),
        addData: {
            title: "",
            mood: "",
            content: "",
            createName: "",
            createTime: "",
            user: "",
            limit:"",
        },
        time:true,
    });
    let methodFun = {
        //自定义时间
        changeTime() {
            console.log(123,status.time);
            status.time = !status.time;
            console.log(1234,status.time);
        },
        // 记录事件
        submitFun:()=> {
            status.addData.createName = status.addData.userName || status.userinfo.userName;
            status.addData.user = status.addData.user || status.userinfo.user;
            status.addData.createTime = status.addData.createTime || GJ_nowTime("date-time","-");
            status.addData.limit = status.userinfo.limit;
            console.log(status.addData, "ddddd");
            if(status.addData.content!="" &&status.addData.title!=""&&status.addData.mood!="" ){
                Api.addData(status.addData).then((res) => {
                    if (res.data) {
                      ElNotification({
                          type: "success",
                          title: "记录成功",
                          message: "本次记录积分+15",
                          duration: 1000,
                      });
                      methodFun.init();
                      getinfo();
                      Api.log({ user: status.userinfo.user, createTime: GJ_nowTime("date-time","-"), type: 1 })
                        .then((res) => {
                          console.log(res.data, "log");
                        });
                    }
                  });
            }else{
                ElNotification({
                    type: "error",
                    title: "记录失败",
                    message: "信息输入不完整、请确认后提交",
                    duration: 1000,
                });
            }
        },
        //初始化表单
        init:()=>{
            status.addData.title = "";
            status.addData.mood = "";
            status.addData.content = "";
            status.addData.createName = "";
            status.addData.createTime = "";
            status.addData.user = "";
            status.addData.limit = "";
        }
    }
    return {
        status,
        methodFun
    }
}

export default dataAddJS;